﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MMDemo
{
    
    public partial class TreeCreatForm : Form
    {
        public TreeCreatForm()
        {
            InitializeComponent();
        }
        Random ran = new Random();
        public const int MAXNUM = 20;
        private List<TreeNode> TreeRoot = new List<TreeNode>();
        private TreeNode[] TreeTable;
        private string tree_table = "";
        private void RamCrebtn_Click(object sender, EventArgs e)
        {
            this.InputStrtext.Text = "";
            tree_table = "";
            TreeRoot.Clear();
            RandCreateTree(TreeRoot);
            TreeTable = new TreeNode[TreeRoot.Count];
            for (int i = 0; i < TreeRoot.Count; ++i)
            {
                TreeTable[i] = new TreeNode();
                TreeTable[i].data = TreeRoot[i].data;
                TreeTable[i].parent = TreeRoot[i].parent;
            }
            List<TreeNode> TempTree = new List<TreeNode>();
            TreeRoot.ForEach(i => TempTree.Add(i));
            tree_table = ConPar_Table(TempTree);
            this.InputStrtext.Text = tree_table;
        }

        private void RandCreateTree(List<TreeNode> root)
        {
            int TreeNodeNum = ran.Next(5, 13);
            for (int i = 0; i < TreeNodeNum; ++i)
            {
                TreeNode tempNode = new TreeNode();
                tempNode.data = ((char)(ran.Next(26) + 65)).ToString();
                if (i == 0)
                {
                    tempNode.parent = -1;
                }
                else
                {
                    tempNode.parent = ran.Next(0, i);
                }
                root.Add(tempNode);
            }
            root.Sort(new ParentComparer());
        }

        public string ConPar_Table(List<TreeNode> root)
        {
            TreeNode str = new TreeNode();
            if (root == null)
            {
                MessageBox.Show("数据为空!");
                return "#";
            }
            for (int i = root.Count - 1; i >= 0; --i)
            {
                if (str.data == "")
                {
                    str = root[i];
                }
                else
                {
                    if (str.parent == root[i].parent)
                    {
                        str.data = str.data + "," + root[i].data;
                    }
                    else
                    {
                        str.data = root[str.parent].data + "(" + str.data + ")";
                        root[str.parent].data = str.data;
                        str.data = "";
                        i++;
                    }
                }
            }
            return root[0].data;
        }

        private void Okbtn_Click(object sender, EventArgs e)
        {
            if (tree_table=="")
            {
                MessageBox.Show("未生成广义表!");
                return;
            }
            GlobalTree.TTData = TreeTable;
            GlobalTree.GenList = tree_table;
            GlobalTree.DataNumber = TreeTable.Length;
            this.Close();
        }
    }
    public class ParentComparer : IComparer<TreeNode>
    {
        public int Compare(TreeNode x, TreeNode y)
        {
            return x.parent.CompareTo(y.parent);
        }
    }
}
